/**
 * COPYRIGHT. Harry Wu 2011. ALL RIGHTS RESERVED.
 * Project: just
 * Author: Harry Wu <harrywu304@gmail.com>
 * Created On: May 30, 2011 10:38:15 AM
 *
*/ 
package org.justframework.ext.oscache;

import org.justframework.core.ClientConnector;
import org.justframework.core.Request;
import org.justframework.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;

/**
 * @author whx
 *
 */
public class OscacheClient implements ClientConnector{
	
	private static Logger logger = LoggerFactory.getLogger(OscacheClient.class);
	
	/**
	 * oscache缓存管理器
	 */
	private static GeneralCacheAdministrator admin = new GeneralCacheAdministrator();
	
	private static Object get(String key) {
		Object value = null;
		try {
			value = admin.getFromCache(key);
		} catch (NeedsRefreshException nre) { 
			admin.cancelUpdate(key);
			logger.debug("OSCache对key[{}]缓存get操作, 引发NeedsRefreshException", key);
		}
		return value;
	}
	
	private static boolean set(String key, Object value) {
		admin.putInCache(key, value);
		return true;
	}
	
	private static boolean delete(String key) {
		admin.removeEntry(key);
		return true;
	}

	/* (non-Javadoc)
	 * @see org.justframework.core.ClientConnector#rpc(org.justframework.core.Request)
	 */
	public Response rpc(Request request) {
		// TODO Auto-generated method stub
		return null;
	}

}
